package com.bawei.es.controller;

import com.bawei.common.core.domain.R;
import com.bawei.es.service.EsIndexService;
import com.bawei.system.domain.dto.EsDocInsertDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/esIndex")
@Api(value = "ES索引管理",tags = "ES索引管理")
public class EsIndexController {

    @Autowired
    private EsIndexService esIndexService;

    /**
     * 根据索引名称创建索引
     *
     * @return
     */
    @PostMapping("/creates")
    @ApiOperation(value = "根据索引名称创建索引")
    public R<Boolean> indexCreate(@RequestBody EsDocInsertDto esDocInsertDto) {
        esIndexService.indexCreates(esDocInsertDto);
        return R.ok();
    }

    @GetMapping("/create")
    public R<Boolean> indexCreate(@RequestParam("indexName") String indexName){
        esIndexService.indexCreate(indexName);
        return R.ok();
    }

    /**
     * 根据索引名称、映射结构创建索引
     *
     * @return
     */
    @PostMapping("/createWithMapping")
    @ApiOperation(value = "根据索引名称、映射结构创建索引")
    public R<Boolean> indexCreateWithMapping(@RequestBody EsDocInsertDto esDocInsertDto) {
        esIndexService.indexCreateWithMapping(esDocInsertDto);
        return R.ok();
    }

    /**
     * 根据索引名称查询索引是否存在
     *
     * @return
     */
    @GetMapping("/indexExit")
    @ApiOperation(value = "根据索引名称查询索引是否存在")
    public R<Boolean> indexExit(@RequestParam("indexName")String indexName) {
        Boolean b = esIndexService.indexExit(indexName);
        return R.ok(b);
    }

    /**
     * 根据索引名称删除索引
     *
     * @return
     */
    @GetMapping("/indexDelete")
    @ApiOperation(value = "根据索引名称删除索引")
    public R<Boolean> indexDelete(@RequestParam("indexName")String indexName) {
        esIndexService.indexDelete(indexName);
        return R.ok();
    }

    /**
     * 根据索引名称获取mapping
     *
     * @return
     */
    @GetMapping("/indexGetMapping")
    @ApiOperation(value = "根据索引名称删除索引")
    public R<Map> indexGetMapping(@RequestParam("indexName")String indexName) {
        esIndexService.indexGetMapping(indexName);
        return R.ok();
    }

    /**
     * 根据索引名称查看详情
     */
    @GetMapping("/indexDetails")
    @ApiOperation(value = "根据索引名称查看详情")
    public R indexDetails(@RequestParam("settings")String indices){
        EsDocInsertDto esDocInsertDto = esIndexService.indexDetails(indices);
        return R.ok(esDocInsertDto);
    }

    @PostMapping("/indexUpdate")
    @ApiOperation(value = "根据索引名称添加修改索引")
    public R indexUpdate(@RequestBody EsDocInsertDto esDocInsertDto,@RequestParam Integer flag){
        esIndexService.indexUpdate(esDocInsertDto,flag);
        return R.ok();
    }

}
